package com.neusoft.bizcore.webauth.secret;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import feign.Contract;
import feign.codec.Decoder;
import feign.codec.Encoder;

@Configuration
@ConditionalOnClass(FeignClientsConfiguration.class)
@EnableFeignClients
@Import(FeignClientsConfiguration.class)
public class AuthFeignClientConfiguration {

    @Autowired
    private BizcoreWebAuthProperties properties;

    private final FeignClientBuilder feignClientBuilder;

    public AuthFeignClientConfiguration(@Autowired final ApplicationContext applicationContext) {
        this.feignClientBuilder = new FeignClientBuilder(applicationContext);
    }

    @Bean
    public WebAuthMicroService authMicroService(final Contract contract, final Decoder decoder, final Encoder encoder) {
        return this.feignClientBuilder.forType(WebAuthMicroService.class, this.properties.getAuthServiceName()).build();
    }
}
